package com.app.worldcupapp.regiter;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.app.worldcupapp.R;
import com.app.worldcupapp.bean.LoginBean;
import com.app.worldcupapp.bean.RegistBean;
import com.app.worldcupapp.bean.UserBean;
import com.app.worldcupapp.login.LoginActivity;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegistActivity extends AppCompatActivity  {
    EditText userNameView,emailView,passwordView,repeatPasswordView;
    Button register;
    String userName,email,password,repeatPassword;
    String TAG = "RegistActivity";
    Boolean result = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        initView();

    register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            initDate();
            if(!(userName.equals("")||email.equals("")||password.equals("")||repeatPassword.equals(""))){
                if(password.equals(repeatPassword)){
                   verify();
                }else {
                    Toast.makeText(RegistActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                }

            }else {
                Toast.makeText(RegistActivity.this, "各项值不能为空", Toast.LENGTH_SHORT).show();
            }

        }
    });
   }
    private void verify() {
        //创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("name",userName)
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url("http://192.168.27.206:8080/app16/user/findone")
                .post(body)
                .build();

        final Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                Gson gson = new Gson();
                RegistBean loginBean = gson.fromJson(data, RegistBean.class);
                Looper.prepare();
                if (loginBean.isSuccess()){

                    Toast.makeText(RegistActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    getData();

                }
                Looper.loop();
            }
        });

    }
    private void initDate() {
       userName =  userNameView.getText().toString();
        email =  emailView.getText().toString();
        password =  passwordView.getText().toString();
        repeatPassword =  repeatPasswordView.getText().toString();

    }

    private void initView() {
        userNameView = findViewById(R.id.userName);
        emailView = findViewById(R.id.email);
        passwordView = findViewById(R.id.password);
        repeatPasswordView = findViewById(R.id.repeatPassword);
        register = findViewById(R.id.register);

    }

    private void getData() {
        //创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("name",userName)
                .add("email",email)
                .add("password",password)
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url("http://192.168.27.206:8080/app16/user/list")
                .post(body)
                .build();

        final Call call = client.newCall(request);


        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();

                Gson gson = new Gson();
                RegistBean registBean = gson.fromJson(data, RegistBean.class);
                Looper.prepare();
                Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
//                Intent intent=new Intent(RegistActivity.this, LoginActivity.class);
//                startActivity(intent);
                Looper.loop();




            }
        });
    }




}